﻿using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace smartphone.App_Code
{
    public class Manufacturer
    {
        static Manufacturer()
        {

        }

        /// <summary>
        /// Lay danh sach toan bo cac hang san xuat
        /// </summary>
        /// <returns></returns>
        public DataTable getManufacturer()
        {
            //khoi tao doi tuong Dbcommand tu class GenericDataAccess
            DbCommand com = GenericDataAccess.CreateCommand();
            //chi dinh storedprocedure se thuc thi
            com.CommandText = "GetManufacturer";
            //thu thi command va tra ket qua ve
            return GenericDataAccess.ExecuteSelectCommand(com);
        }

        /// <summary>
        /// Cap nhat thogn tin hang san xuat
        /// </summary>
        /// <param name="manufacturerID"></param>
        /// <param name="manufacturerName"></param>
        /// <returns></returns>
        public bool UpdateManufacturer(int manufacturerID,  string manufacturerName)
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "UpdateManufacturer";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@maHang";
            param.Value = manufacturerID;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);

            param = comm.CreateParameter();
            param.ParameterName = "@tenHang";
            param.Value = manufacturerName;
            param.DbType = DbType.String;
            param.Size = 50;
            comm.Parameters.Add(param);

            int result = -1;
            result = GenericDataAccess.ExecuteNonQuery(comm);

            return (result != -1);
        }


        /// <summary>
        /// Xoa hang san xuat
        /// </summary>
        /// <param name="manufacturerID"></param>
        /// <returns></returns>
        public bool DeleteManufacturer(int manufacturerID)
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "DeleteManufacturer";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@maHang";
            param.Value = manufacturerID;
            param.DbType = DbType.Int32;
            comm.Parameters.Add(param);


            int result = -1;
            result = GenericDataAccess.ExecuteNonQuery(comm);

            return (result != -1);
        }

        /// <summary>
        /// Tao 1 hang san xuat moi
        /// </summary>
        /// <param name="manufacturerID"></param>
        /// <param name="manufacturerName"></param>
        /// <returns></returns>
        public bool CreateManufacturer(string manufacturerName)
        {
            DbCommand comm = GenericDataAccess.CreateCommand();
            comm.CommandText = "CreateManufacturer";

            DbParameter param = comm.CreateParameter();
            param.ParameterName = "@tenHang";
            param.Value = manufacturerName;
            param.DbType = DbType.String;
            param.Size = 50;
            comm.Parameters.Add(param);

            int result = -1;
            result = GenericDataAccess.ExecuteNonQuery(comm);

            return (result != -1);
        }
    }
}